﻿using HRM.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Reflection;

namespace HRM.Migrator
{
    public static class ModelBuilderExtensions
    {
        public static void ExecuteConfigurations(this ModelBuilder modelBuilder, string assemblyName)
        {
            //取基类=EFModel
            var entityTypes = Assembly.Load(new AssemblyName(assemblyName)).GetTypes()
                        .Where(type => !String.IsNullOrEmpty(type.Namespace) && type.GetTypeInfo().IsClass)
                        .Where(type => type.GetTypeInfo().BaseType != null && (type.GetTypeInfo().BaseType == typeof(EFModel) || type.GetTypeInfo().BaseType.BaseType == typeof(EFModel)));

            foreach (var type in entityTypes)
            {
                modelBuilder.Model.GetOrAddEntityType(type);
            }
        }
    }
}
